package com.wg.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wg.api.dto.OrderDTO;
import com.wg.order.mapper.OrderMapper;
import com.wg.order.pojo.entity.Order;
import com.wg.order.pojo.vo.OrderVO;
import com.wg.order.service.IOrderService;
import com.wg.common.result.PageResult;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zf_serveer
 * @CreateTime 2025/1/11 0011
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Override
    public PageResult queryAllOrders() {
        List<Order> list = lambdaQuery().list();
        return new PageResult(list.size(), BeanUtil.copyToList(list, Order.class));
    }

    @Override
    public OrderVO getOrderById(Long orderId) {
        return BeanUtil.copyProperties(this.getById(orderId), OrderVO.class);
    }

    @Override
    public boolean deleteOrder(Long id) {
        return this.remove(new LambdaUpdateWrapper<>(Order.class).eq(Order::getId, id));
    }

    @Override
    public boolean updateOrder(OrderDTO orderDTO) {
        Order order = BeanUtil.copyProperties(orderDTO, Order.class);
        return this.update(order, new LambdaUpdateWrapper<>(Order.class).eq(Order::getId, order.getId()));
    }

    @Override
    public boolean addOrder(OrderDTO orderDTO) {
        throw new RuntimeException("订单描述不能为空");
//        Order order = BeanUtil.copyProperties(orderDTO, Order.class);
//        if(order.getDesc() == null || order.getDesc().isBlank()){
//            throw new FieldNotEmptyException("订单描述不能为空");
//        }
//        order.setUserId(BaseContext.getCurrentId());
//        order.setOrderNo(BaseContext.getCurrentId() + "_" + Instant.now().toEpochMilli());
//        order.setCreateTime(LocalDateTime.now());
//        return this.save(order);
    }
}
